package ca.uwindsor.cs.deepweb.estimation.method.border;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class TermsWeightDistribution {

	protected Map<Float, Integer> weightdistribution;

	protected DecimalFormat df;

	private float w;

	/**
	 * 
	 */
	public TermsWeightDistribution() {
		// TODO Auto-generated constructor stub
		weightdistribution = new HashMap<Float, Integer>();
	}

	public void addWeight(float weight) {
		w=(float)Math.round(weight*100)/100;  
		Integer v = weightdistribution.get(w);
		if (v == null) {
			weightdistribution.put(w, 1);
		} else {
			v++;
			weightdistribution.put(w, v);
		}
	}
	
	public void calculate(String file) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(file)));
		String line = reader.readLine();
		StringBuilder data = new StringBuilder();
		while (line != null) {
			data.append(line + " ");
			line = reader.readLine();
		}
		String temp = data.toString();
		String[] temp2 = temp.split("\\s");
		for (String s : temp2) {
			addWeight(Float.parseFloat(s));
		}
		reader.close();
	}

	/**
	 * @return the weightdistribution
	 */
	public Map<Float, Integer> getWeightdistribution() {
		return weightdistribution;
	}
	
	public static void main(String[] args){
		args = new String[3];
		args[0]="D:\\Research\\estimation\\Result\\summary\\enwiki_querybasedsampling_3000docs_insample_weight.txt";
		args[1]="D:\\Research\\estimation\\Result\\summary\\enwiki_querybasedsampling_3000docs_insample_weight_x.txt";
		args[2]="D:\\Research\\estimation\\Result\\summary\\enwiki_querybasedsampling_3000docs_insample_weight_y.txt";
		
		TermsWeightDistribution d = new TermsWeightDistribution();
//		d.addWeight(10.004354f);
//		d.addWeight(10.001f);
		try{
		d.calculate(args[0]);
		}catch(IOException e){
			e.printStackTrace();
		}
		Map<Float, Integer> distribution = d.getWeightdistribution();
		StringBuilder builderx = new StringBuilder();
		StringBuilder buildery = new StringBuilder();
		
		for (Entry<Float, Integer> e: distribution.entrySet()) {
			builderx.append(e.getKey());
			builderx.append(" ");
			buildery.append(e.getValue());
			buildery.append(" ");
		}
		
		FileOutputStream fpx;
		FileOutputStream fpy;
		try{
			fpx = new FileOutputStream(args[1]);
			fpx.write(builderx.toString().getBytes());
			fpx.flush();
			fpx.close();
			
			fpy= new FileOutputStream(args[2]);
			fpy.write(buildery.toString().getBytes());
			fpy.flush();
			fpy.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
